<?php defined("MAIN_DIR") or die("No direct access");

error_reporting(0);
$poll_id = UV::readInt($route->getPath(3));
if ($poll_id) {
	$answers_res = mysql_query("select `A`.*,COUNT(`UA`.`ua_id`) as `count` from `answers` as `A` left join `user_answers` as `UA` on `A`.`answer_id` = `UA`.`answer_id` where `A`.`poll_id` = '$poll_id' group by `A`.`answer_id` order by `answer_id`");
	if (mysql_num_rows($answers_res) > 0) {
		require_once CORE."pChart/pData.class";
		require_once CORE."pChart/pChart.class";
		
		$answers = array();
		while ($answers_row = mysql_fetch_array($answers_res)) {
			$answers[] = $answers_row['count'];
		}
		
		$answers_percent = array();
		$hundred = 0;
		for($i=0; $i<count($answers); $i++) {
			$hundred += $answers[$i];
		}
		
		$index = 0;
		$max = $answers[0];
		for($i=0; $i<count($answers)-1; $i++) {
			if ($max < $answers[$i+1]) {
				$max = $answers[$i+1];
				$index = $i+1;
			}
		}
		
		$index++;
		$max = (double)$max / $hundred * 100;
		
		$DataSet = new pData;
		if (100-$max == 0) {
			$DataSet->AddPoint(array($max),"Serie1");
		} else {
			$DataSet->AddPoint(array(100-$max,$max),"Serie1");
		}
		
		$DataSet->AddAllSeries();

		$Test = new pChart(280,220);
		$Test->AntialiasQuality = 0;
		$Test->drawFlatPieGraph($DataSet->GetData(),$DataSet->GetDataDescription(),105,108,100,PIE_NOLABEL,5);
		imageTTFText($Test->Picture, 11, 0, 185, 30, 0, FILES."fonts/arial.ttf", "Лидер: №$index");
		header ("Content-type: image/png");
		$Test->Stroke();
	}
}
?>